<?php

(defined('BASEPATH')) OR exit('No direct script access allowed');

class Invoice_Admin extends Admin_Controller {

    function __construct() {
        
        parent::__construct();
        $this->load->model('invoice/invoice_model');
        $this->load->model('account/account_model');
        $this->load->model('invoice_detail_model');
    }

    function list_invoice() {
        
        $view_data = array();
        $options = $this->_get_search_form_data();

        $this->load->library('pagination');
        
        $config['base_url'] = site_url('invoice-admin/list-invoice');
        $config['total_rows'] = $this->invoice_model->get_invoice_count($options);
        $config = array_merge($config, get_pagination_config());
        
        $this->pagination->initialize($config);

        $options['limit'] = $config['per_page'];
        $options['offset'] = $this->uri->segment(3);
        if (!isset($options['offset']) || !is_numeric($options['offset']) || (int) $options['offset'] < 0 || $options['offset'] > $config['total_rows'])
            $options['offset'] = 0;

        $view_data = $options;
        $view_data['invoices'] = $this->invoice_model->get_invoice($options);
        $view_data['account'] = $this->account_model->get_account_array();
        
        $view_data['pagination'] = $this->pagination->create_links();
        
        $this->load->view('invoice/list', $view_data);
    }

    private function _get_search_form_data() {

        $options = array();

        if ($this->is_postback() && $this->input->post('btnSubmit') === __('submit')) {

            $search = trim(mysql_escape_string($this->input->post('search')));
            
            if ($search != NULL && $search != trim(' '))
                $options['search'] = $search;
            
            $options['year'] = $this->input->post('year');
            $options['month'] = $this->input->post('month');
            $options['day'] = $this->input->post('day');

            $this->phpsession->save('filter', $options);
        }

        $search_session = $this->phpsession->get('filter');

        $options['search'] = (isset($search_session['search'])) ? $search_session['search'] : NULL;
        $options['year'] = isset($search_session['year']) ? $search_session['year'] : date('Y');
        $options['month'] = isset($search_session['month']) ? $search_session['month'] : date('m');
        $options['day'] = isset($search_session['day']) ? $search_session['day'] : date('d');
        
        $options['year_combo']    = get_years_combo(array('combo_name' => 'year', 'from_year' => 2009, 'to_year' => date('Y'), 'year' => $options['year']));
        $options['month_combo']   = get_months_combo(array('combo_name' => 'month', 'month' => $options['month']));
        $options['day_combo']     = get_days_combo(array('combo_name' => 'day', 'day' => $options['day']));


        return $options;
    }
    
    /*---------------------------------------------*/
    
    function delete_invoice() {
        
        if ($this->is_postback()) {
            
            $invoice_id = $this->input->post('invoice_id');
            $this->invoice_detail_model->delete_invoice_detail(array('invoice_id' => $invoice_id));
            $this->invoice_model->delete_invoice(array('id' => $invoice_id));
        }
        redirect('invoice-admin');
    }
    
    function view_invoice_detail() {
        
        if (!$this->is_postback())
            redirect('invoice-admin');

        $view_data = array();
        $invoice_id = $this->input->post('invoice_id');
        $invoice = $this->invoice_model->get_invoice(array('id' => $invoice_id));
        $view_data['invoice'] = $invoice[0];
        $view_data['invoice_detail'] = $this->invoice_detail_model->get_invoice_detail(array('invoice_id' => $invoice_id));
        
        $this->load->model('ticket/ticket_model');
        $view_data['ticket'] = $this->ticket_model->get_ticket_array();
        $view_data['ticket_price'] = $this->ticket_model->get_ticket_array(array('GET_PRICE' => TRUE));

//            $this->load->model('account/account_model'); // ...already loaded in constructor
        $account = $this->account_model->get_account(array('id'=>$invoice[0]->account_id));
        $view_data['account'] = $account[0];

        $this->load->view('invoice/list_invoice_detail', $view_data);
    }

}
